// page/component/new-pages/cart/cart.js
var app = getApp();
Page({
    data: {
        siteInfo: app.siteInfo,
        shop: [],                  // 商店列表
        carts: [],               // 购物车列表
        hasList: false,          // 列表是否有数据
        totalPrice: 0,           // 总价，初始为0
        showLoading: 1,           // 总价，初始为0
        selectItems: [],    // 商品选择容器
        selectAllStatus: false,    // 全选状态，默认全选
        goodsArr: [],//商品对应信息
        obj: {
            name: "hello"
        }
    },
    onLoad: function (e) {
        var that = this;
        app.globalData.showLoading = this.data.showLoading;
        app.initPage({'navigationBarTitleText': '购物车'});
        app.util.request({
            'url': 'user/shopCartList',
            'showLoading': false,
            'data': {'id': that.data.live_id},
            success: function (res) {
                var data = res.data.data;
                var goodsArr = [];
                for (var key in data) {
                    for (var key1 in data[key]['goods']) {
                        goodsArr[data[key]['goods'][key1]['cart_id']] = data[key]['goods'][key1]['goodsInfo'];
                    }
                }
                that.setData({
                    list: data,
                    goodsArr: goodsArr,
                    hasList: app.util.objectLength(data) > 0,
                });
            },
            fail: (res) => {
                wx.showToast({
                    title: '网络或服务器异常',
                })
            },
            complete: (res) => {
                that.setData({
                    showLoading: 0,
                });
                app.globalData.showLoading = 0;
            },
        });
    },
    //下拉刷新
    onPullDownRefresh: function () {
        var that = this;
        var page = getCurrentPages().pop();
        page.onLoad();
        wx.stopPullDownRefresh();
    },
    onShow() {
        this.getTotalPrice();
    },
    /**
     * 当前商品选中事件
     */
    selectGoods(e) {
        var that = this;
        const shop_id = e.currentTarget.dataset.shop_id;
        const cart_id = e.currentTarget.dataset.cart_id;
        var selectItems = this.data.selectItems;

        if (selectItems[cart_id]) {
            delete selectItems[cart_id];
        } else {
            selectItems[cart_id] = 1;
        }

        this.setData({
            selectItems: selectItems
        });

        //全选按钮
        if (app.util.objectLength(that.data.goodsArr) == app.util.objectLength(selectItems)) {
            this.setData({
                selectAllStatus: 1
            });
        } else {
            this.setData({
                selectAllStatus: 0
            });
        }
        this.getTotalPrice();
    },

    /**
     * 删除购物车当前商品
     */
    deleteList(e) {
        var that = this;
        const cart_id = e.currentTarget.dataset.cart_id;
        const shop_id = e.currentTarget.dataset.shop_id;
        var goodsArr = this.data.goodsArr;
        var selectItems = this.data.selectItems;
        var list = this.data.list;
        wx.showModal({
            content: '确定要删除该商品吗',
            success: function (res) {
                if (res.confirm) {
                    app.util.request({
                        'url': 'user/delShopCart',
                        'showLoading': false,
                        'data': {'id': cart_id},
                        success: function (res) {
                            var data = res.data.data;
                            delete goodsArr[cart_id];
                            delete selectItems[cart_id];
                            delete list[shop_id]['goods'][cart_id];
                            if (app.util.objectLength(list[shop_id]['goods']) <= 0) {
                                delete list[shop_id];
                            }
                            that.setData({
                                list: list,
                                goodsArr: goodsArr,
                                hasList: app.util.objectLength(list) > 0,
                            });
                        },
                        fail: (res) => {
                            wx.showToast({
                                title: '网络或服务器异常',
                            })
                        },
                    });
                } else if (res.cancel) {
                }
            }
        })

        this.getTotalPrice();
    },

    /**
     * 购物车全选事件
     */
    selectAll(e) {
        var that = this;
        var selectAllStatus = that.data.selectAllStatus;
        var selectItems = [];
        if (selectAllStatus == 1) {
            this.setData({
                selectItems: selectItems,
                selectAllStatus: 0
            });
            this.getTotalPrice();
            return;
        }

        var goodsArr = this.data.goodsArr;
        for (var key in goodsArr) {
            selectItems[key] = 1;
        }
        this.setData({
            selectItems: selectItems,
            selectAllStatus: 1
        });
        this.getTotalPrice();
    },

    /**
     * 绑定加数量事件
     */
    addCount(e) {
        var that = this;
        const cart_id = e.currentTarget.dataset.cart_id;
        var goodsArr = this.data.goodsArr;

        app.util.request({
            'url': 'user/addShopCartNum',
            'showLoading': false,
            'data': {'id': cart_id},
            success: function (res) {
                var data = res.data.data;
                goodsArr[cart_id].buy_num = goodsArr[cart_id].buy_num * 1 + 1;
                that.setData({
                    goodsArr: goodsArr,
                });
            },
            fail: (res) => {
                wx.showToast({
                    title: '网络或服务器异常',
                })
            },
        });
        this.getTotalPrice();
    },

    /**
     * 绑定减数量事件
     */
    minusCount(e) {
        var that = this;
        const cart_id = e.currentTarget.dataset.cart_id;
        var goodsArr = this.data.goodsArr;

        app.util.request({
            'url': 'user/minusShopCartNum',
            'showLoading': false,
            'data': {'id': cart_id},
            success: function (res) {
                var data = res.data.data;
                goodsArr[cart_id].buy_num = goodsArr[cart_id].buy_num * 1 - 1;
                that.setData({
                    goodsArr: goodsArr,
                });
            },
            fail: (res) => {
                wx.showToast({
                    title: '网络或服务器异常',
                })
            },
        });
        this.getTotalPrice();
    },

    /**
     * 计算总价
     */
    getTotalPrice() {
        var selectItems = this.data.selectItems;
        var goodsArr = this.data.goodsArr;
        var totalPrice = 0;
        for (var key in selectItems) {
            totalPrice += goodsArr[key].goods_amount * goodsArr[key].buy_num;
        }
        this.setData({
            totalPrice: app.util.formatCurrency(totalPrice),
        });
    },

    /**
     * 去结算
     */
    toOrder() {
        var selectItems = this.data.selectItems;
        if (app.util.objectLength(selectItems) <= 0) {
            wx.showToast({
                title: '请选择商品',
                icon: 'none',
            })
            return;
        }
        var cartIds = Object.keys(selectItems);
        wx.setStorageSync('toOrderCardIds', cartIds);//设置去购物车结算的商品id
        wx.redirectTo({
            url: '/wxz_superstore/pages/order/confirm',
        });
    }

})